package com.sword.controller.buy;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.buy.Supplier;
import com.sword.pojo.stock.product.Product;
import com.sword.pojo.stock.product.Productspec;
import com.sword.service.buy.SupplierService;
import com.sword.service.stock.product.ProductService;
import com.sword.service.stock.product.ProductSpecService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/buy")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 供应商渲染
     */
    @RequestMapping("/supplier")
    public String supplier() { return "views/buy/supplier"; }

    /**
     * 查询供应商列表功能
     */
    @RequestMapping("/querySupplier")
    @ResponseBody
    public PageResult querySupplier(@RequestBody QueryPageBean queryPageBean) {
        try {
            return supplierService.querySupplier(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_PRODOCT_FAIL, null, null);
        }
    }

    /**
     * 添加供应商功能
     */
    @ResponseBody
    @RequestMapping("/addSupplier")
    public Result addSupplier(@RequestBody Supplier supplier) {
        try {
            // 判断是否添加成功
            Integer flag = supplierService.addSupplier(supplier);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_PRODOCT_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_PRODOCT_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_PRODOCT_SUCCESS, null);
    }

    @ResponseBody
    @RequestMapping("/editSupplier")
    public Result editSupplier(@RequestBody Supplier supplier) {
        try {
            Integer flag = supplierService.editSupplier(supplier);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_PRODOCT_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_PRODOCT_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_PRODOCT_SUCCESS, null);
    }

    /**
     * 删除商品总分类
     */

    @ResponseBody
    @RequestMapping("/deleteSupplier")
    public Result deleteSupplier(Integer id) {
        try {
            Integer flag = supplierService.deleteSupplier(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_PRODOCT_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_PRODOCT_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_PRODOCT_SUCCESS, null);
    }

    /**
     * 查询所有数据用于可视化
     */
//    @ResponseBody
//    @RequestMapping("/getproduct")
//    public Map getproduct() {
//        List<Product> employees = supplierService.queryAll();
//        Map map = new HashMap();
//        map.put("code", 0);
//        map.put("data",employees);
//        return map;
//    }
}
